package cn.hit.edu.Servlet;

import cn.hit.edu.dao.StudentDao;
import cn.hit.edu.dao.impl.StudentDaoImpl;
import cn.hit.edu.entity.Student;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = "studentServlet", value = "/student-servlet")
public class StuedentServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        String action = request.getParameter("action");
        StudentDao dao = new StudentDaoImpl();
        if(action.equals("add")){
            String sid = request.getParameter("sid");
            String name = request.getParameter("name");
            int age = Integer.parseInt(request.getParameter("age"));
            String birthday = request.getParameter("birthday");
            String gender = request.getParameter("gender");
            Student student = new Student(sid, name, gender, age, birthday);
            dao.add(student);
        }
        else if(action.equals("modify")){
            String sid = request.getParameter("sid");
            String name = request.getParameter("name");
            int age = Integer.parseInt(request.getParameter("age"));
            String birthday = request.getParameter("birthday");
            String gender = request.getParameter("gender");
            Student student = new Student(sid, name, gender, age, birthday);
            dao.modify(student);
        }
        else if(action.equals("remove")){
            String sid = request.getParameter("sid");
            dao.remove(sid);
        }

        response.sendRedirect("index.jsp");

    }
}